package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.CommentReply;
import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.query.CommentReplyQueryObj;
import cn.wolfcode.trip.base.service.ICommentReplyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 回复控制器
 */
@RestController
@RequestMapping("/commentReplies")
public class CommentReplyController {
    @Autowired
    private ICommentReplyService commentReplyService;

    /**
     * 查询回复
     * @param qo
     * @return
     */
    @GetMapping("/{strategyCommentId}/replies")
    public Object selectByStrategyCommentId(@ModelAttribute("qo") CommentReplyQueryObj qo){
        qo.setOrderBy("createTime desc");
        PageInfo pageInfo = commentReplyService.selectByStrategyCommentId(qo);
        return pageInfo;
    }
    @GetMapping("/{strategyCommentId}/count")
    public Object selectCountByStrategyCommentId(@ModelAttribute("qo") CommentReplyQueryObj qo){
        int count = commentReplyService.selectCountByStrategyCommentId(qo);
        return count;
    }

    /**
     * 获取回复我的攻略评论的回复人的基本信息
     * @return
     */
    @GetMapping("/repliers")
    public PageInfo listRepliers(CommentReplyQueryObj qo){
        qo.setPageSize(11);
        qo.setUserId(UserContext.getCurrentUser().getId());
        qo.setOrderBy("cr.createTime desc");
        PageInfo pageInfo=commentReplyService.listRepiersInfo(qo);

        //更新回复信息状态 1代表已经看过了;0代表新的
        commentReplyService.updateState(1,qo.getUserId());
        return pageInfo;
    }
    @PostMapping
    public Object saveReply(CommentReply commentReply){
        JSONResult jsonResult = new JSONResult();
        try {
            commentReply.setUser(UserContext.getCurrentUser());
            commentReplyService.saveReply(commentReply);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }


}
